﻿using System;
using System.IO;
using System.Xml;
using NAnt.Core;
using NAnt.Core.Attributes;

namespace ECM7.NantContrib.Types
{
	/// <summary>
	/// Данные в формате Xml
	/// </summary>
	[Serializable, ElementName("raw-xml")]
	public class RawXmlData : DataTypeBase
	{
		[TaskAttribute("file", Required = false), StringValidator(AllowEmpty = false)]
		public FileInfo File { get; set; }

		protected override bool CustomXmlProcessing
		{
			get { return true; }
		}

		public XmlNode GetXml()
		{
			XmlNode clone = XmlNode.FirstChild.Clone();
			ExpandProperties(clone);

			return clone;
		}

		/// <summary>
		/// Обрабатываем свойства в атрибутах
		/// </summary>
		private void ExpandProperties(XmlNode node)
		{
			if (node == null)
			{
				return;
			}

			if (node.Attributes != null)
			{
				foreach (XmlAttribute attribute in node.Attributes)
				{
					attribute.Value = Project.ExpandProperties(attribute.Value, Location);
				}
			}

			foreach (XmlNode child in node.ChildNodes)
			{
				ExpandProperties(child);
			}
		}
	}
}
